package com.itcat.controller;

import com.itcat.common.CommonPage;
import com.itcat.common.CommonResult;
import com.itcat.common.ResultCode;
import com.itcat.exception.BusinessException;
import com.itcat.interceptor.UserHolder;
import com.itcat.mysql.param.UmsAdminLoginParam;
import com.itcat.mysql.param.UmsAdminParam;
import com.itcat.mysql.param.UpdatePasswordParam;
import com.itcat.mysql.pojo.UmsAdmin;
import com.itcat.mysql.pojo.UmsRole;
import com.itcat.service.AdminCacheService;
import com.itcat.service.UmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * [后台用户管理]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/3/30 13:21]
 */
@RestController
@RequestMapping("/admin")
public class UsmAdminController {

    @Autowired
    private UmsAdminService umsAdminService;


    /***
     * 注册
     * @author Cr Mr
     * @date 2022/3/30 13:52
     */
    @PostMapping("/register")
    public CommonResult<UmsAdmin> register(@RequestBody UmsAdminParam umsAdminParam) {
        UmsAdmin umsAdmin = umsAdminService.register(umsAdminParam);
        return CommonResult.success(umsAdmin);
    }

    /***
     * 登录以后返回token
     * @author Cr Mr
     * @date 2022/3/30 14:28
     */
    @PostMapping("/login")
    public CommonResult login(@RequestBody UmsAdminLoginParam umsAdminLoginParam) {
        String token = umsAdminService.login(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());
        if (token == null) {
            throw new BusinessException(ResultCode.LOGIN_FAILED);
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", "");
        return CommonResult.success(tokenMap);
    }

    /***
     * 获取当前登录用户信息
     * @author Cr Mr
     * @date 2022/4/1 21:33
     */
    @GetMapping("/info")
    public CommonResult info(@RequestParam(required = false) String name) {
        String username = name;
        if (name == null || name.equals("")) {
            username = UserHolder.getUsername();
        }
        Map map = umsAdminService.info(username);
        return CommonResult.success(map);
    }

    @Autowired
    private AdminCacheService adminCacheService;

    /***
     * 登出功能
     * @author Cr Mr
     * @date 2022/4/2 9:20
     */
    @PostMapping("logout")
    public CommonResult logout() {
        adminCacheService.removeAdmin(UserHolder.get());
        return CommonResult.success();
    }

    /***
     * 刷新token
     * @author Cr Mr
     * @date 2022/4/2 9:54
     */
    @GetMapping("refreshToken")
    public CommonResult refreshToken() {
        String token = umsAdminService.refreshToken();
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", "");
        return CommonResult.success(tokenMap);
    }

    /***
     * 根据用户名或姓名分页获取用户列表
     * @author Cr Mr
     * @date 2022/4/2 12:09
     */
    @GetMapping("list")
    public CommonResult list(@RequestParam(required = false) String keyword,
                             @RequestParam(required = false) Integer pageNum,
                             @RequestParam(required = false) Integer pageSize) {
        CommonPage<UmsAdmin> page = umsAdminService.getPage(keyword, pageNum, pageSize);
        return CommonResult.success(page);
    }

    /***
     * 删除指定用户信息
     * @author Cr Mr
     * @date 2022/4/2 14:56
     */
    @PostMapping("delete/{id}")
    public CommonResult delete(@PathVariable Long id) {
        umsAdminService.delete(id);
        return CommonResult.success();
    }

    /***
     * 给用户分配角色
     * @author Cr Mr
     * @date 2022/4/2 15:29
     */
    @PostMapping("role/update")
    public CommonResult reoleUpdate(Long adminId, Long[] roleIds) {
        umsAdminService.reoleUpdate(adminId, roleIds);
        return CommonResult.success();
    }

    /***
     * 获取指定用户的角色
     * @author Cr Mr
     * @date 2022/4/2 19:09
     */
    @GetMapping("role/{adminId}")
    public CommonResult getByRole(@PathVariable Long adminId) {
        List<UmsRole> list = umsAdminService.getByRole(adminId);
        return CommonResult.success(list);
    }

    /***
     * 修改指定用户信息
     * @author Cr Mr
     * @date 2022/4/2 19:22
     */
    @PostMapping("update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody UmsAdmin admin) {
        umsAdminService.update(admin);
        return CommonResult.success();
    }

    /***
     * 修改指定用户密码
     * @author Cr Mr
     * @date 2022/4/2 19:43
     */
    @PostMapping("updatePassword")
    public CommonResult updatePassword(@RequestBody UpdatePasswordParam updatePasswordParam) {
        umsAdminService.updatePassword(updatePasswordParam);
        return CommonResult.success();
    }

    /***
     * 修改帐号状态
     * @author Cr Mr
     * @date 2022/4/2 20:54
     */
    @PostMapping("updateStatus/{id}")
    public CommonResult updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        umsAdminService.updateStatus(id, status);
        return CommonResult.success();
    }

    /***
     * 获取指定用户信息
     * @author Cr Mr
     * @date 2022/4/2 21:12
     */
    @GetMapping("/{id}")
    public CommonResult getUmsAdmin(@PathVariable Long id) {
        UmsAdmin admin = umsAdminService.getUmsAdmin(id);
        return CommonResult.success(admin);
    }

}
